//
//  Global.m
//  pai
//
//  Created by pro on 14-10-28.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "Global.h"

@implementation Global

@synthesize logined;

+ (Global *)instance {
    if (!instance) {
        instance = [[super allocWithZone:NULL] init];
    }
    return instance;
}


- (id)init {
    self = [super init];
    
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
    CFRelease(uuid_ref);
    self.uuid = [NSString stringWithString:(__bridge NSString*)uuid_string_ref];
    CFRelease(uuid_string_ref);
    
    self.fixurl = SYSTEMDOMAIN;
    self.useurl = SYSTEMDOMAIN;
    
    return self;
}

-(NSString*) fixHttpUrl:(NSString*)url
{
    NSString *httpurl = @"http://";
    httpurl = [httpurl stringByAppendingString:self.fixurl];
    httpurl = [httpurl stringByAppendingString:url];
    
    return httpurl;
}

-(NSString*) useHttpUrl:(NSString *)url
{
    NSString *httpurl = @"http://";
    httpurl = [httpurl stringByAppendingString:self.useurl];
    httpurl = [httpurl stringByAppendingString:url];
    
    return httpurl;
}


- (NSString *)systemDocumentPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    
    return documentDirectory;
}

-(NSString *)systemLibraryPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    
    documentDirectory = [documentDirectory stringByAppendingString:@"/Caches/appDate"];
    [self createDirectory:documentDirectory];
    
    return documentDirectory;
}

- (void)createDirectory:(NSString *)path
{
    NSFileManager* fileManager = [NSFileManager defaultManager];
    BOOL isDir;
    BOOL existed = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) )
    {
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

- (long long) filesize: (NSString*)filepath
{
    long long _fileSize = 0;
    NSFileManager* filemanager = [NSFileManager defaultManager];
    BOOL isDirectory;
    BOOL isExists = [filemanager fileExistsAtPath:filepath isDirectory:&isDirectory];
    if(isExists && !isDirectory){
        NSDictionary * attributes = [filemanager attributesOfItemAtPath:filepath error:nil];
        // file size
        NSNumber *theFileSize = [attributes objectForKey:NSFileSize];
        _fileSize= [theFileSize longLongValue];
    }
    return _fileSize;
}

- (void) deleteFile: (NSString*)filepath
{
    NSFileManager* filemanager = [NSFileManager defaultManager];
    //    NSError *error;
    [filemanager removeItemAtPath:filepath error:nil];
}

- (void) moveFile: (NSString*) src to: (NSString*) dest isDirectory:(BOOL)is
{
    NSFileManager* filemanager = [NSFileManager defaultManager];
    
    BOOL destIsDirectory;
    BOOL srcIsDircetory;
    BOOL destIsExists = [filemanager fileExistsAtPath:dest isDirectory:&destIsDirectory];
    BOOL srcIsDirectory = [filemanager fileExistsAtPath:src isDirectory:&srcIsDircetory];
    
    
    if (srcIsDirectory && srcIsDircetory==NO && NO==destIsExists && destIsDirectory==NO) {
        [filemanager moveItemAtPath:src toPath:dest error:nil];
    }
}

- (BOOL) fileIsExsits: (NSString*) filepath
{
    NSFileManager* filemanager = [NSFileManager defaultManager];
    BOOL isDirectory;
    BOOL isExsits = [filemanager fileExistsAtPath:filepath isDirectory:&isDirectory];
    if (isDirectory==NO && isExsits) {
        return YES;
    }
    return NO;
}

- (BOOL) dirIsExsits: (NSString*) filepath
{
    NSFileManager* filemanager = [NSFileManager defaultManager];
    BOOL isDirectory;
    BOOL isExsits = [filemanager fileExistsAtPath:filepath isDirectory:&isDirectory];
    if (isDirectory && isExsits) {
        return YES;
    }
    return NO;
}

// 图片缓存文件
- (NSString*) imageCacheFile: (NSString*) name
{
    NSString* cachePath = [self cachePath];
    NSString* imageCachePath = [cachePath stringByAppendingString:@"/image"];
    [self createDirectory:imageCachePath];
    NSString* filepath = [NSString stringWithFormat:@"%@/%@", imageCachePath, name];
    return filepath;
}

- (NSString*) cachePath
{
    NSString* path = [[self systemLibraryPath] stringByAppendingString:@"/cache"];
    [self createDirectory:path];
    
    return path;
}

- (NSString*) htmlsDir
{
    NSString* htmlsPath = [[self systemLibraryPath] stringByAppendingString:@"/htmls"];
    [self createDirectory:htmlsPath];
    return htmlsPath;
}

@end

@implementation NSString (md5)

-(NSString *) md5HexDigest
{
    const char *original_str = [self UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, strlen(original_str), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < 16; i++)
        [hash appendFormat:@"%02X", result[i]];
    return [hash uppercaseString];
}
@end
